package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.exception.CustomException;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.vo.MybatisReviewVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**
     * =========================================================新增数据============================================
     */
    //新增接口1
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult save(@PathVariable String name,@PathVariable int age,@PathVariable String sex) {
        int save = reviewService.save(name, age, sex);
        return AjaxResult.success("成功插入:" + save + "条数据");
    }

    //新增接口2
    @PostMapping("/saveData")
    public AjaxResult save(@RequestBody MybatisReviewVO mybatisReviewVO) {
        Review review = new Review();
        BeanUtils.copyProperties(mybatisReviewVO, review);
        int save = reviewService.PostSave(review);
        return AjaxResult.success("成功插入:" + save + "条数据");
    }

    /**
     * =========================================================删除数据=============================================
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult deleteById(@PathVariable Long id) {
        int a = reviewService.deleteById(id);
        return AjaxResult.success("成功删除:" + a + "条数据");
    }
    /**
     * =========================================================修改数据=============================================
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody MybatisReviewVO mybatisReviewVO) {
        Review review = new Review();
        BeanUtils.copyProperties(mybatisReviewVO, review);
        reviewService.update(review);
        return AjaxResult.success("修改成功");
    }
    /**
     * =========================================================查询数据=============================================
     */
    @GetMapping("/getById")
    public AjaxResult findById(@RequestParam("id") Long id) {
        try {
            Review review = reviewService.findById(id);
            return AjaxResult.success(review);
        } catch (Exception e) {
            throw new CustomException("查询失败");
        }
    }

    /**
     * =========================================================分页查询=============================================
     */
    @GetMapping("/getDataByPage")
    //public TableDataInfo page(@RequestParam(required = false, defaultValue = "1") int pageNum,
    //                          @RequestParam(required = false, defaultValue = "10") int pageSize) {
    //    List<Review> pageList = reviewService.page(pageNum, pageSize);
    //    return getDataTable(pageList);
    public TableDataInfo page(Review review) {
        startPage();
        List<Review> list = reviewService.page(review);
        return getDataTable(list);
    }

}